<?php
	/******Function : hornbill Base Class******/
	/******Author : Feifei******/
	/******Date : 12/04/2008 ****/

	require_once('class.db.php');

	class base{
		var $tableName ; //Selected Table Name
		var $execSQL = "";
		var $execObj;
		var $execResult;

		function __construct($obj){
			global $wbTableHash;  //Table Hash
			$this->tableName = $wbTableHash[$obj];
		}

		function __destruct(){

		}

		function __set($name , $value){
			$this->$name = $value;
		}

		function __get($name){
			return $this->$name;
		}

		function __call($method , $param){

		}

		/**@ $term: array , int**/
		function safePara($term){
			if(is_array($term)){
				$len = count($term);
				for($i=0;$i<$len;$i++){
					if(!get_magic_quotes_gpc())		$term[$i] = addslashes($term[$i]);
				}
			}
			else if(is_int($term)||is_string($term)){
				$term = trim($term);
				if(!$term)	return;
				if(!get_magic_quotes_gpc())		$term = addslashes($term);
			}
			return $term;
		}
		
		function __getSubSql($arr){
			$temp = array();
			foreach($arr as $key=>$val){
				$str= $key."='".$val."'";
				array_push($temp , $str);
			}
			return $temp;
		}

		function __generateSql(){
			global $wbTableHash;
			$etype = $this->execObj[0]["type"];
			$sql = "";
			switch($etype){
				
				/****ADD****/
				case 0 : 
					$vArr = $this->execObj[1];
					foreach($vArr as $val){
						$temp .= " '".$val."' , ";
					}
					$value = "( ".substr($temp , 0 , -2)." )";
					$sql = "insert into ".$this->tableName." values ".$value;
					break;

				/****UPDATE****/
				case 1:
					$set = implode(" , " , $this->__getSubSql($this->execObj[1]));
					$where = implode(" and " , $this->__getSubSql($this->execObj[2]));
					$stable = isset($this->execObj[3])?($wbTableHash[$this->execObj[3][0]]):($this->tableName);
					$sql = "update ".$stable." set ".$set." where ".$where;
					break;

				/****DELETE****/
				case 2:
					$value = implode(" and " , $this->__getSubSql($this->execObj[1]));
					$sql = "delete from ".$this->tableName." where ".$value;
					break;

				/****SELECT****/
				case 3:
					$vars = implode(" , " , $this->execObj[1]);
					$conds = implode(" and " , $this->__getSubSql($this->execObj[2]));
					$options =implode(" " , $this->execObj[3]);
					$sql = "select ".$vars." from ".$this->tableName." where ".$conds." ".$options;
					break;
				
				/**** COUNT ***/
				case 4:
					$sql = "select count(*) from ".$this->tableName;
					break;
			}
			$this->execSQL = $sql;
		}

		function __execSql(){
			global $warabiDb;
			$warabiDb->connect();
			$this->__generateSql();
			$result = $warabiDb->query($this->execSQL);
			$this->execResult = $result;
		}

		function __insertOneResult(){
			global $warabiDb;
			$this->__execSql();
			$sql = "SELECT last_insert_id() as id from ".$this->tableName." LIMIT 1";
			$result = $warabiDb->query($sql);
			$tempr = $warabiDb->rowResult($result);
			$lastid = $tempr[0];
			$warabiDb->disconnect();
			return $lastid;
		}

		function __getAssocResult(){
			global $warabiDb;
			$this->__execSql();
			$arr = array();
			while($temp = $warabiDb->assocResult($this->execResult)){
				array_push($arr , $temp);
			}
			$warabiDb->freeResult($this->execResult);
			$warabiDb->disconnect();
			return $arr;
		}

		function __getRowResult(){
			global $warabiDb;
			$this->__execSql();
			$arr = array();
			while($temp = $warabiDb->rowResult($this->execResult)){
				array_push($arr , $temp);
			}
			$warabiDb->freeResult($this->execResult);
			$warabiDb->disconnect();
			return $arr;
		}

		function __updateOneResult(){
			global $warabiDb;
			$this->__execSql();
			return $this->execResult;
			
		}
		
		function __deleteOneResult(){
			global $warabiDb;
			$this->__execSql();
			return $this->execResult;
		}



		function __getTableNum(){
			global $warabiDb;
			$this->__execSql();
			$tempr = $warabiDb->rowResult($this->execResult);
			return $tempr[0];
		}

		function __execSqlCom($sql){
			global $warabiDb;
			$warabiDb->connect();
			$result = $warabiDb->query($sql);
			$this->execResult = $result;
			return $result;
		}

	}

?>